package com.colabo.j2ee.web.core.service.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.ibatis.SqlMapClientTemplate;

import com.colabo.j2ee.web.core.context.ContextUtil;
import com.colabo.j2ee.web.core.dao.mediator.IDefaultMediator;
import com.colabo.j2ee.web.core.exception.MediatorException;
import com.colabo.j2ee.web.core.exception.ServiceException;
import com.colabo.j2ee.web.core.servertime.IServerTime;
import com.colabo.j2ee.web.core.service.IDefaultService;
import com.colabo.j2ee.web.core.vo.RecordObject;

/**
 * Service层的默认接口.虽然属于Service层，但并不涉及任何业务，原则上仅仅对持久层操作如增删,条件查询,更新等操进行转换
 */
public class DefaultServiceImpl implements IDefaultService {
	private IServerTime serverTime;

	public IServerTime getServerTime() {
		return serverTime;
	}

	public void setServerTime(IServerTime serverTime) {
		this.serverTime = serverTime;
	}

	/**
	 * 注入的IDefaultMediator实现类 如有需要，也可以在配置文件中替换掉平台内置实现的DefaultMediator
	 */
	private IDefaultMediator defaultMediator;

	public IDefaultMediator getDefaultMediator() {
		return defaultMediator;
	}

	public void setDefaultMediator(IDefaultMediator defaultMediator) {
		this.defaultMediator = defaultMediator;
	}

	/**
	 * 提供获取容器环境内注册Bean的方法
	 * 
	 * @param beanName
	 *            注册的Bean的编号
	 * @return
	 */
	public Object getBean(String beanName) {
		return ContextUtil.getBean(beanName);
	}

	public int deleteByID_Hibernate_Object(Class cls, Serializable id) throws ServiceException {
		int rs = 0;
		try {
			rs = defaultMediator.deleteByID_Hibernate_Object(cls, id);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public int delete_Hibernate_ComposedQuery(String composedSQL, Object[] params) throws ServiceException {
		int rs = 0;
		try {
			rs = defaultMediator.delete_Hibernate_ComposedQuery(composedSQL, params);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public int delete_Hibernate_NamedQuery(String queryID, Object[] params) throws ServiceException {
		int rs = 0;
		try {
			rs = defaultMediator.delete_Hibernate_NamedQuery(queryID, params);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public int delete_Hibernate_Object(RecordObject vo) throws ServiceException {
		int rs = 0;
		try {
			rs = defaultMediator.delete_Hibernate_Object(vo);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public int delete_JDBC(String sql, Object[] params) throws ServiceException {
		int rs = 0;
		try {
			rs = defaultMediator.delete_JDBC(sql, params);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public int delete_iBatis(String queryID, Object[] params) throws ServiceException {
		int rs = 0;
		try {
			rs = defaultMediator.delete_iBatis(queryID, params);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public List findAll_Hibernate_ComposedHQL(String composedHQL) throws ServiceException {
		List rs = null;
		try {
			rs = defaultMediator.findAll_Hibernate_ComposedHQL(composedHQL);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public List findAll_Hibernate_NamedQuery(String queryID) throws ServiceException {
		List rs = null;
		try {
			rs = defaultMediator.findAll_Hibernate_NamedQuery(queryID);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public List findAll_Hibernate_Object(Class cls) throws ServiceException {
		List rs = null;
		try {
			rs = defaultMediator.findAll_Hibernate_Object(cls);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public List findAll_JDBC(String sql) throws ServiceException {
		List rs = null;
		try {
			rs = defaultMediator.findAll_JDBC(sql);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public List findAll_iBatis(String queryID) throws ServiceException {
		List rs = null;
		try {
			rs = defaultMediator.findAll_iBatis(queryID);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public RecordObject findByID_Hibernate_ComposedHQL(String composedHQL, Serializable id) throws ServiceException {
		RecordObject rs = null;
		try {
			rs = defaultMediator.findByID_Hibernate_ComposedHQL(composedHQL, id);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public RecordObject findByID_Hibernate_Get(Class cls, Serializable id) throws ServiceException {
		RecordObject rs = null;
		try {
			rs = defaultMediator.findByID_Hibernate_Get(cls, id);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public RecordObject findByID_Hibernate_Load(Class cls, Serializable id) throws ServiceException {
		RecordObject rs = null;
		try {
			rs = defaultMediator.findByID_Hibernate_Load(cls, id);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public RecordObject findByID_Hibernate_NamedQuery(String queryID, Serializable id) throws ServiceException {
		RecordObject rs = null;
		try {
			rs = defaultMediator.findByID_Hibernate_NamedQuery(queryID, id);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public RecordObject findByID_iBatis(String queryID, Serializable id) throws ServiceException {
		RecordObject rs = null;
		try {
			rs = defaultMediator.findByID_iBatis(queryID, id);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public Object[] find_Hibernate_ComposedHQL(String hql, String[] propertyNames, String[] operators, Object[] values,
			int offset, int size, boolean isTotalSize, String orderBy, String groupBy, String otherCause)
			throws ServiceException {
		Object[] rs = null;
		try {
			rs = defaultMediator.find_Hibernate_ComposedHQL(hql, propertyNames, operators, values, offset, size,
					isTotalSize, orderBy, groupBy, otherCause);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public Object[] find_Hibernate_NamedQuery(String queryName, Object[] params, int offset, int size,
			boolean isTotalSize) throws ServiceException {
		Object[] rs = null;
		try {
			rs = defaultMediator.find_Hibernate_NamedQuery(queryName, params, offset, size, isTotalSize);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public List find_iBatis(String queryID, Object para, int offset, int size) throws ServiceException {
		List rs = null;
		try {
			rs = defaultMediator.find_iBatis(queryID, para, offset, size);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public List find_iBatis(final String queryID, final Object para) throws ServiceException {
		List rs = null;
		try {
			rs = defaultMediator.find_iBatis(queryID, para);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public HibernateTemplate getHibernateTemplate() throws ServiceException {
		HibernateTemplate rs = null;
		try {
			rs = defaultMediator.getHibernateTemplate();
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public JdbcTemplate getJdbcTemplate() throws ServiceException {
		JdbcTemplate rs = null;
		try {
			rs = defaultMediator.getJdbcTemplate();
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public SqlMapClientTemplate getSqlMapClientTemplate() throws ServiceException {
		SqlMapClientTemplate rs = null;
		try {
			rs = defaultMediator.getSqlMapClientTemplate();
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public int save_Hibernate_ComposedQuery(String composedSQL, Object[] params) throws ServiceException {
		int rs = 0;
		try {
			rs = defaultMediator.save_Hibernate_ComposedQuery(composedSQL, params);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public int save_Hibernate_Object(RecordObject vo) throws ServiceException {
		int rs = 0;
		try {
			rs = defaultMediator.save_Hibernate_Object(vo);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public int save_JDBC(String sql, Object[] params) throws ServiceException {
		int rs = 0;
		try {
			rs = defaultMediator.save_JDBC(sql, params);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public int save_iBatis(String queryID, Object[] params) throws ServiceException {
		int rs = 0;
		try {
			rs = defaultMediator.save_iBatis(queryID, params);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public int update_Hibernate_ComposedQuery(String composedSQL, Object[] params) throws ServiceException {
		int rs = 0;
		try {
			rs = defaultMediator.update_Hibernate_ComposedQuery(composedSQL, params);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public int update_Hibernate_NamedQuery(String queryID, Object[] params) throws ServiceException {
		int rs = 0;
		try {
			rs = defaultMediator.update_Hibernate_NamedQuery(queryID, params);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public int update_Hibernate_Object(RecordObject vo) throws ServiceException {
		int rs = 0;
		try {
			rs = defaultMediator.update_Hibernate_Object(vo);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public int update_JDBC(String sql, Object[] params) throws ServiceException {
		int rs = 0;
		try {
			rs = defaultMediator.update_JDBC(sql, params);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}

	public int update_iBatis(String queryID, Object[] params) throws ServiceException {
		int rs = 0;
		try {
			rs = defaultMediator.update_iBatis(queryID, params);
		} catch (MediatorException me) {
			throw new ServiceException(me);
		}
		return rs;
	}
}
